package com.publicClass;

import com.util.Vessel;

import java.util.List;
import java.util.Scanner;

public class User {

    long id;
    String name;
    String password;
    String Sex;
    int age;

    //联系方式
    String contract;

    //专业
    String major;
    //身份
    Role role;
    //课程
    String schoolClass;
    //宿舍
    int dormitoryId;
    //年级
    String grade;

    public User() {
    }

    //学生构造函数
    public User(long id, String name, String password,String sex, int age, String contract, String major, Role role, String schoolClass, int dormitoryId, String grade) {
        this.id = id;
        this.name = name;
        this.password = password;
        Sex = sex;
        this.age = age;
        this.contract = contract;
        this.major = major;
        this.role = Role.Student;
        this.schoolClass = schoolClass;
        this.dormitoryId = dormitoryId;
        this.grade = grade;
    }

    public String toStringL1(){
        return getId() + "\t\t\t" + getName() + "\t\t\t" + getPassword() + "\t\t\t" + getSex() + "\t\t\t" + getAge()
                + "\t\t\t" + getContract() + "\t\t\t" + getMajor() + "\t\t\t" + getRole() + "\t\t\t" + getSchoolClass()
                + "\t\t\t" + getDormitoryId() + "\t\t\t" + getGrade();
    }


    //老师构造函数
    public User(long id, String name, String password, String sex, int age, String contract, String major, Role role) {
        this.id = id;
        this.name = name;
        this.password = password;
        Sex = sex;
        this.age = age;
        this.contract = contract;
        this.major = major;
        this.role = Role.Teacher;
    }

    //管理员构造函数
    public User(long id, String name, String password,String contract, Role role) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.contract = contract;
        this.role = Role.Manager;
    }
    public String toStringL3(){
        return getId() + "\t\t\t" + getName() + "\t\t\t" + getPassword() + "\t\t\t" + getContract() + "\t\t\t" + getRole();
    }


    public User(long id, String name, String sex, int age, String contract, String major, Role role, String schoolClass, int dormitoryId, String grade) {
        this.id = id;
        this.name = name;
        Sex = sex;
        this.age = age;
        this.contract = contract;
        this.major = major;
        this.role = role;
        this.schoolClass = schoolClass;
        this.dormitoryId = dormitoryId;
        this.grade = grade;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return Sex;
    }

    public void setSex(String sex) {
        Sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getContract() {
        return contract;
    }

    public void setContract(String contract) {
        this.contract = contract;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getSchoolClass() {
        return schoolClass;
    }

    public void setSchoolClass(String schoolClass) {
        this.schoolClass = schoolClass;
    }

    public int getDormitoryId() {
        return dormitoryId;
    }

    public void setDormitoryId(int dormitoryId) {
        this.dormitoryId = dormitoryId;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    //修改用户信息
    public static boolean updateUser(){
        Scanner scanner = new Scanner(System.in);
        boolean isFlag = true;
        do {
            User user = User.findUser();
            System.out.println("请输入修改后的姓名");
            String newName = scanner.next();
            System.out.println("请输入用户性别");
            String sex = scanner.next();
            System.out.println("请输入用户年龄");
            int age = scanner.nextInt();
            System.out.println("请输入修改后的联系方式");
            String newContract = scanner.next();
            System.out.println("请输入专业");
            String major = scanner.next();
            System.out.println("请输入修改后的用户身份： 1：管理员  2：老师  3：学生");
            int i = scanner.nextInt();
            //将需要更改的身份设为空重新选择
            user.role = null;
            switch (i){
                case 1:
                    user.setRole(Role.Manager);
                    break;
                case 2:
                    user.setRole(Role.Teacher);
                    break;
                case 3:
                    user.setRole(Role.Student);
                    break;
            }
            System.out.println("请输入修改后的班级");
            String schoolClass = scanner.next();
            System.out.println("请输入修改后的宿舍号");
            int dormitory = scanner.nextInt();
            System.out.println("请输入修改后的年级");
            String grade = scanner.next();
            User user1 = new User(user.getId(),newName,sex,age,newContract,major,user.getRole(),schoolClass,dormitory,grade);
            //修改
            CRUD crud = new CRUD();
            crud.update(user1,user,Vessel.users);
            isFlag = false;
        }while (isFlag);
        return isFlag;
    }

    //添加用户信息
    public static boolean addUser(){
        Scanner scanner = new Scanner(System.in);
        boolean isFlag = true;
        do {
            long id = Vessel.id;
            id++;
            System.out.println("请输入您要添加的用户名");
            String name = scanner.next();
            System.out.println("请输入初始密码");
            String password = scanner.next();
            System.out.println("请输入用户性别");
            String sex = scanner.next();
            System.out.println("请输入用户年龄");
            int age = scanner.nextInt();
            System.out.println("请输入用户联系方式");
            String contract = scanner.next();
            System.out.println("请输入专业");
            String major = scanner.next();
            System.out.println("请选择身份:1.管理员 2.教师 3.学生");
            Role role = null;
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    role=Role.Manager;
                    break;
                case 2:
                    role=Role.Teacher;
                    break;
                case 3:
                    role=Role.Student;
                    break;
            }
            User newUser = new User(id,name,password,sex,age,contract,major,role);
            Vessel.users.add(newUser);
            System.out.println("请问是否继续添加(y/n)");
            String next = scanner.next();
            if (next.equals("y")){
                isFlag=true;
            }else if (next.equals("n")){
                isFlag=false;
            }else {
                System.out.println("输入有误，请重新操作");
                isFlag = false;
            }
        }while (isFlag);
        return isFlag;
    }
    //删除用户信息
    public static boolean delUser(){
        Scanner scanner = new Scanner(System.in);
        boolean isFlag = true;
        do {
            System.out.println("请输入您要删除的用户id");
            long l = scanner.nextLong();
            for (int i = 0; i < Vessel.users.size(); i++) {
                if (Vessel.users.get(i).getId() == l){
                    Vessel.users.remove(i);
                    break;
                }
            }
        }while (isFlag);
        return isFlag;
    }


    //根据id查找用户信息
    public static User findUser() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要查询的用户id");
        long l = scanner.nextLong();
        for (int i = 0; i < Vessel.users.size(); i++) {
            if (Vessel.users.get(i).getId() == l) {
                System.out.println(Vessel.users.get(i).toString());
                return Vessel.users.get(i);
            }
        }
        return null;
    }

    //根据角色查找用户
    public static void findRole(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择您需要查找的用户身份");
        System.out.println("1.管理员");
        System.out.println("2.教师");
        System.out.println("3.学生");
        int i = scanner.nextInt();
        switch (i){
            case 1:
                for (User user:Vessel.users) {
                    if (user.getRole() == Role.Manager){
                        System.out.println(user.toString());
                    }
                }
                break;
            case 2:
                for (User user:Vessel.users) {
                    if (user.getRole() == Role.Teacher){
                        System.out.println(user.toString());
                    }
                }
                break;
            case 3:
                for (User user:Vessel.users) {
                    if (user.getRole() == Role.Student){
                        System.out.println(user.toString());
                    }
                }
                break;
        }
    }
    public String toString(){
        return getId()+"\t\t\t"+getName()+"\t\t\t"+getPassword()+"\t\t\t"+getSex()+"\t\t\t"+getAge()+"\t\t\t"+getContract()+"\t\t\t"+getMajor()+"\t\t\t"+getRole();
    }
}
